using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using Budgeteer.Services.Common;
using Budgeteer.Services.DataProviders;
using Budgeteer.Services.Domain;
using Xunit;

namespace Test.UnitTests.Budgeteer.Services.DataProviders
{
    public class CategoryDataProviderFacts
    {
        public class GetCategoriesAmount : IDisposable
        {
            readonly TransactionScope transaction;

            public GetCategoriesAmount()
            {
                transaction = BaseDataProvider.CreateTransactionScope();
            }

            [Fact]
            public void WillGetTotalAmountWithinTimePeriodForEachCategory()
            {
                CategoryDataProvider provider = new CategoryDataProvider(Config.ConnectionString);

                IList<CategoryDto> categories = provider.GetCategoriesAmount(new DateTime(2004,9,1), new DateTime(2004,10,1));

                Assert.Equal(11,categories.Count);
                Assert.Equal(1, categories.Count(c => c.Name == "GROCERIES"));
                Assert.Equal(115.62M,categories.Where(c => c.Name == "GROCERIES").Select(c => c.TotalAmount).Single());
            }

            [Fact]
            public void WillGetListOfAllCategoryMaskPairs()
            {
                CategoryDataProvider provider = new CategoryDataProvider(Config.ConnectionString);

                IDictionary<string,string> categoryMasks = provider.GetAllCategoryMaskPairs();

                Assert.Equal(14, categoryMasks.Count);
                Assert.Equal(1,categoryMasks.Count(c => c.Key == "GROCERIES" && c.Value == "SAFEWAY|CTRL MKT"));
            }

            public void Dispose()
            {
                transaction.Dispose();
            }
        }
    }

    public class StubCategoryDataProvider: CategoryDataProvider
    {
        public IList<CategoryDto> GetCategoriesAmount_Result;
        public DateTime GetCategoriesAmount_StartDate;
        public DateTime GetCategoriesAmount_EndDate;

        public StubCategoryDataProvider()
            : base ("")
        {}

        public override IList<CategoryDto> GetCategoriesAmount(DateTime startDate, DateTime endDate)
        {
            GetCategoriesAmount_StartDate = startDate;
            GetCategoriesAmount_EndDate = endDate;

            return GetCategoriesAmount_Result;
        }
     }
}